{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2.4 \u5b57\u7b26\u4e32\u5339\u914d\u548c\u641c\u7d22\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u5339\u914d\u6216\u8005\u641c\u7d22\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u672c"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u5339\u914d\u7684\u662f\u5b57\u9762\u5b57\u7b26\u4e32\uff0c\u90a3\u4e48\u4f60\u901a\u5e38\u53ea\u9700\u8981\u8c03\u7528\u57fa\u672c\u5b57\u7b26\u4e32\u65b9\u6cd5\u5c31\u884c\uff0c\n\u6bd4\u5982 str.find() , str.endswith() , str.startswith() \u6216\u8005\u7c7b\u4f3c\u7684\u65b9\u6cd5\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "text = 'yeah, but no, but yeah, but no, but yeah'\n# Exact match\ntext == 'yeah'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Match at start or end\ntext.startswith('yeah')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "text.endswith('no')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Search for the location of the first occurrence\ntext.find('no')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u590d\u6742\u7684\u5339\u914d\u9700\u8981\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u548c re \u6a21\u5757\u3002\n\u4e3a\u4e86\u89e3\u91ca\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u57fa\u672c\u539f\u7406\uff0c\u5047\u8bbe\u4f60\u60f3\u5339\u914d\u6570\u5b57\u683c\u5f0f\u7684\u65e5\u671f\u5b57\u7b26\u4e32\u6bd4\u5982 11/27/2012 \uff0c\u4f60\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "text1 = '11/27/2012'\ntext2 = 'Nov 27, 2012'\nimport re\n# Simple matching: \\d+ means match one or more digits\nif re.match(r'\\d+/\\d+/\\d+', text1):\nprint('yes')\nelse:\nprint('no')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "if re.match(r'\\d+/\\d+/\\d+', text2):\nprint('yes')\nelse:\nprint('no')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u4f7f\u7528\u540c\u4e00\u4e2a\u6a21\u5f0f\u53bb\u505a\u591a\u6b21\u5339\u914d\uff0c\u4f60\u5e94\u8be5\u5148\u5c06\u6a21\u5f0f\u5b57\u7b26\u4e32\u9884\u7f16\u8bd1\u4e3a\u6a21\u5f0f\u5bf9\u8c61\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "datepat = re.compile(r'\\d+/\\d+/\\d+')\nif datepat.match(text1):\nprint('yes')\nelse:\nprint('no')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "if datepat.match(text2):\nprint('yes')\nelse:\nprint('no')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "match() \u603b\u662f\u4ece\u5b57\u7b26\u4e32\u5f00\u59cb\u53bb\u5339\u914d\uff0c\u5982\u679c\u4f60\u60f3\u67e5\u627e\u5b57\u7b26\u4e32\u4efb\u610f\u90e8\u5206\u7684\u6a21\u5f0f\u51fa\u73b0\u4f4d\u7f6e\uff0c\n\u4f7f\u7528 findall() \u65b9\u6cd5\u53bb\u4ee3\u66ff\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "text = 'Today is 11/27/2012. PyCon starts 3/13/2013.'\ndatepat.findall(text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5b9a\u4e49\u6b63\u5219\u5f0f\u7684\u65f6\u5019\uff0c\u901a\u5e38\u4f1a\u5229\u7528\u62ec\u53f7\u53bb\u6355\u83b7\u5206\u7ec4\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "datepat = re.compile(r'(\\d+)/(\\d+)/(\\d+)')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6355\u83b7\u5206\u7ec4\u53ef\u4ee5\u4f7f\u5f97\u540e\u9762\u7684\u5904\u7406\u66f4\u52a0\u7b80\u5355\uff0c\u56e0\u4e3a\u53ef\u4ee5\u5206\u522b\u5c06\u6bcf\u4e2a\u7ec4\u7684\u5185\u5bb9\u63d0\u53d6\u51fa\u6765\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m = datepat.match('11/27/2012')\nm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Extract the contents of each group\nm.group(0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.group(1)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.group(2)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.group(3)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.groups()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "month, day, year = m.groups()\n# Find all matches (notice splitting into tuples)\ntext"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "datepat.findall(text)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for month, day, year in datepat.findall(text):\nprint('{}-{}-{}'.format(year, month, day))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "findall() \u65b9\u6cd5\u4f1a\u641c\u7d22\u6587\u672c\u5e76\u4ee5\u5217\u8868\u5f62\u5f0f\u8fd4\u56de\u6240\u6709\u7684\u5339\u914d\u3002\n\u5982\u679c\u4f60\u60f3\u4ee5\u8fed\u4ee3\u65b9\u5f0f\u8fd4\u56de\u5339\u914d\uff0c\u53ef\u4ee5\u4f7f\u7528 finditer() \u65b9\u6cd5\u6765\u4ee3\u66ff\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for m in datepat.finditer(text):\nprint(m.groups())"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5173\u4e8e\u6b63\u5219\u8868\u8fbe\u5f0f\u7406\u8bba\u7684\u6559\u7a0b\u5df2\u7ecf\u8d85\u51fa\u4e86\u672c\u4e66\u7684\u8303\u56f4\u3002\n\u4e0d\u8fc7\uff0c\u8fd9\u4e00\u8282\u9610\u8ff0\u4e86\u4f7f\u7528re\u6a21\u5757\u8fdb\u884c\u5339\u914d\u548c\u641c\u7d22\u6587\u672c\u7684\u6700\u57fa\u672c\u65b9\u6cd5\u3002\n\u6838\u5fc3\u6b65\u9aa4\u5c31\u662f\u5148\u4f7f\u7528 re.compile() \u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u5b57\u7b26\u4e32\uff0c\n\u7136\u540e\u4f7f\u7528 match() , findall() \u6216\u8005 finditer() \u7b49\u65b9\u6cd5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u5199\u6b63\u5219\u5f0f\u5b57\u7b26\u4e32\u7684\u65f6\u5019\uff0c\u76f8\u5bf9\u666e\u904d\u7684\u505a\u6cd5\u662f\u4f7f\u7528\u539f\u59cb\u5b57\u7b26\u4e32\u6bd4\u5982 r'(\\d+)/(\\d+)/(\\d+)' \u3002\n\u8fd9\u79cd\u5b57\u7b26\u4e32\u5c06\u4e0d\u53bb\u89e3\u6790\u53cd\u659c\u6760\uff0c\u8fd9\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u4e2d\u662f\u5f88\u6709\u7528\u7684\u3002\n\u5982\u679c\u4e0d\u8fd9\u6837\u505a\u7684\u8bdd\uff0c\u4f60\u5fc5\u987b\u4f7f\u7528\u4e24\u4e2a\u53cd\u659c\u6760\uff0c\u7c7b\u4f3c '(\\\\d+)/(\\\\d+)/(\\\\d+)' \u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9700\u8981\u6ce8\u610f\u7684\u662f match() \u65b9\u6cd5\u4ec5\u4ec5\u68c0\u67e5\u5b57\u7b26\u4e32\u7684\u5f00\u59cb\u90e8\u5206\u3002\u5b83\u7684\u5339\u914d\u7ed3\u679c\u6709\u53ef\u80fd\u5e76\u4e0d\u662f\u4f60\u671f\u671b\u7684\u90a3\u6837\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m = datepat.match('11/27/2012abcdef')\nm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "m.group()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u7cbe\u786e\u5339\u914d\uff0c\u786e\u4fdd\u4f60\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4ee5$\u7ed3\u5c3e\uff0c\u5c31\u50cf\u8fd9\u4e48\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "datepat = re.compile(r'(\\d+)/(\\d+)/(\\d+)$')\ndatepat.match('11/27/2012abcdef')\ndatepat.match('11/27/2012')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\uff0c\u5982\u679c\u4f60\u4ec5\u4ec5\u662f\u505a\u4e00\u6b21\u7b80\u5355\u7684\u6587\u672c\u5339\u914d/\u641c\u7d22\u64cd\u4f5c\u7684\u8bdd\uff0c\u53ef\u4ee5\u7565\u8fc7\u7f16\u8bd1\u90e8\u5206\uff0c\u76f4\u63a5\u4f7f\u7528 re \u6a21\u5757\u7ea7\u522b\u7684\u51fd\u6570\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "re.findall(r'(\\d+)/(\\d+)/(\\d+)', text)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f46\u662f\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u4f60\u6253\u7b97\u505a\u5927\u91cf\u7684\u5339\u914d\u548c\u641c\u7d22\u64cd\u4f5c\u7684\u8bdd\uff0c\u6700\u597d\u5148\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u518d\u91cd\u590d\u4f7f\u7528\u5b83\u3002\n\u6a21\u5757\u7ea7\u522b\u7684\u51fd\u6570\u4f1a\u5c06\u6700\u8fd1\u7f16\u8bd1\u8fc7\u7684\u6a21\u5f0f\u7f13\u5b58\u8d77\u6765\uff0c\u56e0\u6b64\u5e76\u4e0d\u4f1a\u6d88\u8017\u592a\u591a\u7684\u6027\u80fd\uff0c\n\u4f46\u662f\u5982\u679c\u4f7f\u7528\u9884\u7f16\u8bd1\u6a21\u5f0f\u7684\u8bdd\uff0c\u4f60\u5c06\u4f1a\u51cf\u5c11\u67e5\u627e\u548c\u4e00\u4e9b\u989d\u5916\u7684\u5904\u7406\u635f\u8017\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}